<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\shop;

use app\api\controller\shop\Base;
use think\Db;

/**
 * @title 【VUE首页】
 */
class Index extends Base {

    protected function _initialize() {

        if (isset($_SERVER["HTTP_ORIGIN"])) {
            header('Access-Control-Allow-Origin:' . $_SERVER["HTTP_ORIGIN"]);
        }
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:OPTIONS, GET, POST');
        header('Access-Control-Max-Age:86400');

        header('Access-Control-Allow-Credentials:true');

        header('Access-Control-Allow-Headers:x-requested-with,content-type,custom-header,tk-uid,tk-token');
    }

    /**
     * @title 首页
     * @method post
     * @params cid
     * @params last_index 0 初始查询记录
     * @params page_size 10 分页数
     */
    public function home() {


        $cid = input('post.cid', 0);

        $last_index = input('post.last_index', 0);
        $page_size = input('post.page_size', 10);

        if ($cid) {
            $where = [
                'cid' => ['=', $cid],
                'recommend' => ['=', 1],
                'status' => ['=', 1]
            ];
        } else {
            $where = [
                'recommend' => ['=', 1],
                'status' => ['=', 1]
            ];
        }


        $last_index = input('post.last_index', 0);
        $page_size = input('post.page_size', 20);

        $lists = model('goods')
                ->field('id,cid as category,image,title,description,price')
                ->where($where)
                ->limit($last_index, $page_size)
                ->order('recommend desc,listorder asc')
                // ->fetchSql(true)
                ->select();

        foreach ($lists as $key => $value) {
            $lists[$key]['description'] = strip_tags($value['description']);
            $lists[$key]['image'] = APP_HOST . img_resize($value['image'], 200, 200);
        }




        // 首页幻灯片
        $slides = Db::name('nav')
                        ->where('cid', 'IN', function($query) {
                            $query->name('nav_cat')->where('alias', 'shop_banner')->field('id');
                        })
                        ->order('listorder', 'asc')->column('image');


        //   $slides = db('nav')->where('cid', $nav_cid)->order('listorder', 'asc')->column('image');

        foreach ($slides as $key => $value) {
            $slides[$key] = APP_HOST . img_resize($value, 500, 0);
        }



        return ['code' => 0, 'msg' => 'success', 'data' => compact('lists', 'slides')];
    }

    /**
     * @title 分类列表
     * @method post
     */
    public function category() {

        $lists = db('goods_cat')
                ->order('listorder asc,id ')
                ->field('id,title,image')
                ->select();

        return ['code' => 0, 'msg' => 'success', 'data' => compact('lists')];
    }

    /**
     * @title 商品列表
     * @method post
     * @params cid 商品的分类id 
     */
    public function goods() {

        $cid = input('post.cid', 0);

        $lists = db('goods')
                        ->where('status', 1)
                        ->where('cid', $cid)
                        ->order('id')->field('id,title,image,price')->select();

        foreach ($lists as $key => $value) {
            $lists[$key]['image'] = APP_HOST . img_resize($value['image'], 200, 200);
        }

        return ['code' => 0, 'msg' => 'success', 'data' => compact('lists')];
    }

}
